using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using RideMe.Core;
using System.Threading;
using RideMe.Presentation.Properties;

namespace RideMe.Presentation
{
    public partial class GoToFileDialog : DialogBase
    {
        public GoToFileDialog()
        {
            InitializeComponent();
            FileSystemService.FileFound += new EventHandler<ProjectFileEventArgs>(FileSystemService_FileFound);
        }

        /// <summary>
        /// Adds the selected file to the Open Files in the project.
        /// </summary>
        private void OpenSelectedFile()
        {
            if (matchingFilesListBox.SelectedItem != null)
            {
                ProjectService.AddOpenFile(((Pair<string,string>)matchingFilesListBox.SelectedItem).Second);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageService.ShowInformationalMessage(this, Resources.NoItemSelectedMessage);
            }
        }

        /// <summary>
        /// Handles the FileFound event of the FileSystemService object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileSystemService_FileFound(object sender, ProjectFileEventArgs e)
        {
            matchingFilesListBox.Items.Add(new Pair<string,string>(e.FileName, e.FilePath));
        }

        /// <summary>
        /// Handles the TextChanged event of the fileNameTextBox control.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileNameTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                matchingFilesListBox.Items.Clear();
                FileSystemService.LookForFile(ProjectService.ProjectPath, fileNameTextBox.Text);
            }
            catch (ArgumentException)
            {
                MessageService.ShowInformationalMessage(this, Resources.InvalidFilePatternMessage);
                fileNameTextBox.Clear();
                fileNameTextBox.Focus();
            }
        }

        /// <summary>
        /// Handles the Click evcent of the okButton.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void okButton_Click(object sender, EventArgs e)
        {
            OpenSelectedFile();
        }

        /// <summary>
        /// Handles the OnClosing event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            FileSystemService.FileFound -= new EventHandler<ProjectFileEventArgs>(FileSystemService_FileFound);
        }

        /// <summary>
        /// Handles the MouseDownClick event of the matchingFilesListBox control.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void matchingFilesListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OpenSelectedFile();
        }

    }
}